﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Taopi.WebComponents
{
    /// <summary>
    ///ProductThumbnail 的摘要说明
    /// </summary>
    public class ProductThumbnail : WebControl
    {
        const string productcategorykey = "productcategory";
        const string productnamekey = "productname";
        const string productpricekey = "productprice";

        public ProductThumbnail()
            : base("div")
        { }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            AddCssLink();
        }

        private void AddCssLink()
        {
            var controls = Page.Header.Controls.OfType<HtmlLink>().ToArray();

            string href = ResolveUrl("~/Styles/ProductThumbnail.css");
            for (int i = 0; i < controls.Length; i++)
            {
                if (controls[i].Href == href)
                    return;
            }

            var link = new HtmlLink { Href = href };
            link.Attributes.Add("rel", "stylesheet");
            link.Attributes.Add("type", "text/css");
            Page.Header.Controls.Add(link);
        }

        public string ProductName
        {
            get { return (string)ViewState[productnamekey]; }
            set { ViewState[productnamekey] = value; }
        }
        public string ProductCategory
        {
            get { return (string)ViewState[productcategorykey]; }
            set { ViewState[productcategorykey] = value; }
        }
        public string ProductPrice
        {
            get { return (string)ViewState[productpricekey]; }
            set { ViewState[productpricekey] = value; }
        }

        public string ProductId
        {
            get { return (string)ViewState["ProductId"]; }
            set { ViewState["ProductId"] = value; }
        }

        public string CoverPhoto
        {
            get
            {
                if (ViewState["CoverPhoto"] == null)
                    return null;
                else
                    return (string)ViewState["CoverPhoto"];
            }
            set { ViewState["CoverPhoto"] = value; }
        }

        public string Owner
        {
            get
            {
                if (ViewState["Owner"] == null)
                    return null;
                else
                    return (string)ViewState["Owner"];
            }
            set { ViewState["Owner"] = value; }
        }

        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
            base.RenderContents(writer);

            string coverPhotoPath;
            if (string.IsNullOrEmpty(CoverPhoto))
                coverPhotoPath = "~/Images/product.png";
            else
                coverPhotoPath = string.Format("~/userFiles/{0}/{1}", Owner, CoverPhoto);

            writer.Write("<img  class=\"productCoverPhoto\" alt=\"{0}\" src=\"{1}\" />", ProductName, ResolveUrl(coverPhotoPath));

            writer.Write("<span class=\"productname\"><a href=Product.aspx?id=" + ProductId + ">" + ProductName + "</a></span>");

            writer.Write("<span>" + ViewState[productcategorykey] + "</span>");
            writer.Write("<span>" + ViewState[productpricekey] + "</span>");

        }
    }
}